import React from 'react';
import { fetchInstitutions } from '../service';

const useInstitutions = companyId => {
    const [outData, setOutData] = React.useState([]);
    const [loading, setLoading] = React.useState(false);

    const fetchData = async () => {
        setLoading(true);
        const { data = [], code } =
            (await fetchInstitutions({ institutionType: 'personnel', expire: '0', companyId })) || {};

        if (code === 10000) {
            const finalData = data.map(item => ({
                value: item.id,
                label: item.institutionName
            }));
            setOutData(finalData);
        }
        setLoading(false);
    };

    React.useEffect(() => {
        if (companyId) {
            fetchData();
        }
    }, [companyId]);

    return [outData, loading];
};

export default useInstitutions;
